<style scoped>
.layui-row {
    overflow: hidden;
}

.select-item {
    width: 34.333333%;
    float: right;
}

.layui-row:after,
.layui-row:before {
    clear: none;
}

.layui-form>>>.layui-form-select {
    z-index: inherit;
}
</style>

<template>
<div class="organization">
    <template v-if="permission.permis.indexOf(41) > -1">
    <div>
        <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>添加种植户</a>
    </div>
    </template>
    <form class="layui-form ">
        <div class="layui-col-lg4 select-item" style="text-align: right;width: auto">
            <div class="layui-inline" style="width: 200px">
                <select type="text" name="userid" id="farmer" class="layui-select">
                    <option value="">请选择种植户</option>
                    <option v-for="item in farmers" :value="item.guid">{{item.name}}</option>
                </select>
            </div>
            <div class="layui-inline" style="width: 200px">
                <select type="text" name="depart" id="depart" class="layui-select">
                    <option value="">请选择部门</option>
                    <option v-for="item in departments" :value="item.guid">{{item.name}}</option>
                </select>
            </div>
            <div class="layui-inline" style="width: 200px">
                <select name="status" id="status" style="width:250px">
                    <option value="">请选择状态</option>
                    <option value="1">启用</option>
                    <option value="2">禁用</option>
                </select>
            </div>
            <button type="button" class="layui-btn sou" @click="search">搜索</button>
        </div>
    </form>
    <table class="layui-table layui-form">
        <thead>
            <tr>
                <th>种植户</th>
                <th>电话</th>
                <th>所属部门</th>
                <th>地址</th>
                <th>状态</th>
                <th>创建时间</th>
                <template v-if="permission.permis.indexOf(41) > -1">
                <th>操作</th>
                </template>
            </tr>
        </thead>
        <tbody>
            <tr class="id9" v-for="(val, index) in listdata" :key="index">
                <td>{{val.name}}</td>
                <td>{{val.telephone}}</td>
                <td>{{val.depname}}</td>
                <td>{{val.province}}{{val.city}}{{val.district}} {{val.address}}</td>
                <td><template v-if="val.status == 1">启用</template><template v-if="val.status == 2">禁用</template></td>
                <td>{{val.createtime}}</td>
                <template v-if="permission.permis.indexOf(41) > -1">
                <td><a href="javascript:;" @click="edit(val.guid)">修改</a> | <a href="javascript:;" @click="del(val.guid)">删除</a></td>
                </template>
            </tr>
        </tbody>
    </table>

    <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage"></Pagination>
    <div style="height: 200px"></div>
</div>
</template>

<script>
import Pagination from '../common/Pagination.vue'
export default {
    name: 'FarmerInfo',
    components: {
        Pagination
    },
    data() {
        return {
            postData: {
                functiontype: 24,
                pageNo: 1,
                pageSize: 10
            },
            listdata: [],
            currentPage: 1,
            totalPage: 0,
            searchName: '',
            departments: [],
            farmers: [],
            permission: JSON.parse(sessionStorage.getItem('permissions')),
        }
    },
    mounted() {
        let that = this;
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer;
            that.getPageData();
            that.getDepartment();
            that.getUsers();
        });
    },
    updated: function () {
        layui.form.render('select');
        layui.form.render();
    },
    methods: {
        getUsers: function () {
            let that = this;
            this.axios({
                method: 'get',
                url: '/farmer/Farmerorder!getHasPermissionFarmers.action',
                params: {}
            }).then(response => {
                if (response.data.msg == "success") {
                    that.farmers = response.data.farmers;
                }  else {
                    this.toastText('获取种植户数据失败');
                }
            });
        },
        getDepartment: function () {
            let that = this;
            this.axios({
                method: 'get',
                responseType: 'json',
                url: '/sales/Salespermission!getPermisForDepart.action',
                params: {functionTypes: 24, departype: 3} //lrj-0221修改，原来不传functype值
            }).then(response => {
                if (response.data.msg == 'true') {
                    that.departments = response.data.department;
                }else {
                    this.toastText('获取部门数据失败');
                }
            });
        },
        search: function () {
            this.getPageData('search');
        },
        turnPage: function (data) {
            this.currentPage = data;
            this.getPageData();
        },
        getPageData: function (type = '') {
            let that = this;
            let loader = layui.layer.load();

            let postData = this.postData;
            if (type != 'search') {
                postData.pageNo = that.currentPage;
            } else {
                postData.pageNo = 1;
            }
            postData.name = $("#qaname").val();
            postData.userid = $("#farmer").val()
            postData.deptguid = $("#depart").val()
            postData.status = $("#status").val()

            $.ajax({
                url: '/farmer/Farmerinfo!list.action',
                type: "post",
                data: postData,
                dataType: "json",
                success: function (res) {
                    if (res.msg == 'success') {
                        //分页数据赋值
                        // that.currentPage = res.info.indexPage;
                        that.totalPage = res.pagelist.pageCount;
                        that.listdata = res.pagelist.rows;
                    } else {
                        that.alert("系统错误");
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });
        },
        add: function () {
            this.$parent.switchPage('AddFarmerInfo');
        },
        edit: function (id) {
            this.$parent.gid = id;
            this.$parent.switchPage('EditFarmerInfo');
        },
        del: function (id) {
            let that = this;
            layer.confirm('确定要删除该数据？', function () {
                let loader = layer.load();
                let postParam = {};
                postParam['entity.guid'] = id;
                $.ajax({
                    url: '/farmer/Farmerinfo!del.action',
                    type: "post",
                    data: postParam,
                    dataType: "json",
                    success: function (res) {
                        if (res.msg == "success") {
                            that.toast("删除成功");
                            that.getPageData();
                        } else if (res.msg == 'canotdel') {
                            that.toast('有订单，不能删除！');
                        } else {
                            that.toast('系统错误！');
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });

            })
        },
    }
}
</script>
